package vn.aptech.leaveManagementSystem.view.general;

import groovyjarjarantlr.collections.impl.Vector;

import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vn.aptech.leaveManagementSystem.business.dto.Application;
import vn.aptech.leaveManagementSystem.business.dto.Employee;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ViewHistoryLeaveForm extends JFrame{
	private JTable tbHistory;
	private DefaultTableModel modelHistory;
	private int idEmpParameter;
	private MyModel mymodel=new MyModel();
	private JButton btnClose;
	private JLabel lblDepartment;
	private JLabel lblName;
	private JTextField txtfName;
	private JTextField txtfDepartment;
	private JLabel lblPosition;
	private JTextField txtfPosition;
	public ViewHistoryLeaveForm(int idEmp) {
		idEmpParameter=idEmp;
		initGUI();
		initData();
		initAction();
	}

	private void initAction() {
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Close();
			}
		});
		tbHistory.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					if (e.getClickCount() == 2) {
						if (tbHistory.getSelectedRow() != -1) {
							java.util.Vector<Object> v=(java.util.Vector<Object>) modelHistory.getDataVector().elementAt(tbHistory.getSelectedRow());
							ViewApplicationForm viewApp=new ViewApplicationForm((Integer) v.get(0));
							viewApp.setVisible(true);
							viewApp.setAlwaysOnTop(true);
						}
					}
				}
			}
		});
	}

	private void initData() {
		UpdateDataTableHistory();	
		UpdateInfoOwner();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	/**
	 * 
	 */
	private void initGUI() {
		setTitle("View History Leave");
		setBounds(100, 100, 1000, 700);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(null);
		setLocationRelativeTo(null);
		//---------------------------------------------------
		tbHistory = new JTable();
		JScrollPane spHistory=new JScrollPane(tbHistory);
		spHistory.setBounds(10, 97, 972, 558);
		getContentPane().add(spHistory);
		modelHistory=new DefaultTableModel(){
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		modelHistory.addColumn("ID App.");
		modelHistory.addColumn("Start Date");
		modelHistory.addColumn("End Date");
		modelHistory.addColumn("Reason");
		modelHistory.addColumn("Contact");
		tbHistory.setModel(modelHistory);
		tbHistory.getColumnModel().getColumn(0).setPreferredWidth(10);
		tbHistory.getColumnModel().getColumn(1).setPreferredWidth(20);
		tbHistory.getColumnModel().getColumn(2).setPreferredWidth(20);
		tbHistory.getColumnModel().getColumn(3).setPreferredWidth(230);
		tbHistory.getColumnModel().getColumn(4).setPreferredWidth(50);
		
		btnClose = new JButton("Close");
		btnClose.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		btnClose.setBounds(285, 7, 107, 23);
		getContentPane().add(btnClose);
		
		lblName = new JLabel("Name:");
		lblName.setBounds(10, 11, 81, 14);
		getContentPane().add(lblName);
		
		lblDepartment = new JLabel("Department:");
		lblDepartment.setBounds(10, 41, 81, 14);
		getContentPane().add(lblDepartment);
		
		txtfName = new JTextField();
		txtfName.setEditable(false);
		txtfName.setBounds(91, 7, 194, 20);
		getContentPane().add(txtfName);
		txtfName.setColumns(10);
		
		txtfDepartment = new JTextField();
		txtfDepartment.setEditable(false);
		txtfDepartment.setColumns(10);
		txtfDepartment.setBounds(91, 36, 194, 20);
		getContentPane().add(txtfDepartment);
		
		lblPosition = new JLabel("Position:");
		lblPosition.setBounds(10, 72, 81, 14);
		getContentPane().add(lblPosition);
		
		txtfPosition = new JTextField();
		txtfPosition.setEditable(false);
		txtfPosition.setBounds(91, 66, 194, 20);
		getContentPane().add(txtfPosition);
		txtfPosition.setColumns(10);
		//---------------------------------------------------
	}
	
	public void UpdateDataTableHistory()
	{
		try {
			ResultSet rs=mymodel.applications.getLeaveHistory(idEmpParameter);
			while (rs.next())
			{
				Application tempApp=MyParse.toApplication(rs);
				java.util.Vector<Object> v=new java.util.Vector<Object>();
				v.add(tempApp.getIDApplication());
				v.add(tempApp.getStartDate().toString());
				v.add(tempApp.getEndDate().toString());
				v.add(tempApp.getReason());
				v.add(tempApp.getContact());
				modelHistory.insertRow(tbHistory.getRowCount(),v);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void UpdateInfoOwner()
	{
		try {
			ResultSet rs= mymodel.employees.getEmployee(idEmpParameter);
			if (rs.next())
			{
				txtfName.setText(rs.getString("FirstName")+" "+rs.getString("LastName"));
				txtfDepartment.setText(rs.getString("DepartmentName"));
				txtfPosition.setText(rs.getString("PositionName"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Close()
	{
		toBack();
		dispose();
	}
}
